package tr.edu.yildiz.ce.dap.appagent.command.deployment.agent;

import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.AbstractAgentCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.CommandConstants;
import tr.edu.yildiz.ce.dap.appagent.exception.AgentRetryException;
import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.process.DeploymentProcess;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;
import tr.edu.yildiz.ce.dap.appagent.util.AgentProperties;

public class RegisterCommand extends AbstractAgentCommand implements CommandConstants{

	public RegisterCommand(AbstractProcess process) {
		super(process);
	}

	@Override
	protected void prepareRequest(DapMessage msg) throws BasicAgentException {
		msg.setCommand(REGISTER);
		msg.setAgentName(AgentProperties.getInstance().getName());
		msg.setAgentListeningPort((AgentProperties.getInstance().getListeningPort()));
	}
	
	@Override
	protected void processResponse(DapMessage responseMsg) throws BasicAgentException{
		if (responseMsg.getResponse().equals(RESPONSE.OK)){
			getProcess().getProcessManager().createListenerProcess(AgentProperties.getInstance().getListeningPort().intValue());
		}else {
			throw new AgentRetryException("Registration failed.Response : ",responseMsg.getResponse() );
		}
		
	}

	@Override
	public DapMessage rollback(DapMessage msg) throws BasicAgentException {
		// TODO Auto-generated method stub
		return null;
	}

}
